package hu.elte.tudaskezelo.dbpedialike.activity;

import hu.elte.tudaskezelo.dbpedialike.R;
import hu.elte.tudaskezelo.dbpedialike.domain.CityFind;
import hu.elte.tudaskezelo.dbpedialike.ws.OwnResultReceiver;
import hu.elte.tudaskezelo.dbpedialike.ws.OwnResultReceiver.Receiver;
import hu.elte.tudaskezelo.dbpedialike.ws.QueryService;
import hu.elte.tudaskezelo.dbpedialike.ws.SPARQLQuery;

import java.util.ArrayList;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.inject.Inject;

public class FindCityActivity extends RoboActivity implements Receiver {

	final String tag = "FindCityActivity";

	@Inject
	public SPARQLQuery sparqlQuery;

	@InjectView(R.id.searchField)
	EditText searchField;
	@InjectView(R.id.citySelector)
	Spinner citySelector;
	@InjectView(R.id.progress)
	ProgressBar progressBar;
	@InjectView(R.id.search)
	Button search;
	@InjectView(R.id.jump)
	Button jump;
	@InjectView(R.id.citySelectorText)
	TextView citySelectorText;

	public OwnResultReceiver mReceiver;
	Receiver receiver = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchcity);

		jump.setEnabled(false);
		// search.setEnabled(false);
		citySelector.setVisibility(View.INVISIBLE);
		citySelectorText.setVisibility(View.INVISIBLE);
		progressBar.setVisibility(View.INVISIBLE);

		jump.setOnClickListener(new jumpListener());
		search.setOnClickListener(new searchListener());
		searchField.addTextChangedListener(new searchTextWatcher());
	}

	private class jumpListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			CityFind selectedItem = (CityFind) citySelector.getSelectedItem();

			double longitude = selectedItem.getLongitude();
			double latitude = selectedItem.getLatitude();
			Log.d(tag, longitude + " " + latitude);

			Intent intent = new Intent();
			intent.putExtra("longitude", longitude);
			intent.putExtra("latitude", latitude);
			setResult(2, intent);
			finish();
		}
	}

	private class searchListener implements OnClickListener {
		@Override
		public void onClick(View view) {

			mReceiver = new OwnResultReceiver(new Handler());
			mReceiver.setReceiver(FindCityActivity.this);

			Intent intent = new Intent(getApplicationContext(),
					QueryService.class);
			intent.putExtra("receiver", mReceiver);
			intent.putExtra("command", "findCityQuery");
			intent.putExtra("queryParam", searchField.getText());
			startService(intent);

		}
	}

	private class searchTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable s) {
			// if (s.length() > 2) {
			// search.setEnabled(true);
			// } else {
			// search.setEnabled(false);
			// }

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

	}

	private static final int RUNNING = 0;
	private static final int FINISHED = 1;
	private static final int ERROR = 2;

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case RUNNING:
			Log.d(tag, "Query running!");
			progressBar.setVisibility(View.VISIBLE);
			break;
		case FINISHED:
			Log.d(tag, "Query finished!");

			ArrayList<Parcelable> results = resultData
					.getParcelableArrayList("queryResults");
			ArrayList<CityFind> arraySpinner = new ArrayList<CityFind>();

			if (results != null && results.size() > 0) {
				jump.setEnabled(true);
				for (Parcelable iterator : results) {
					arraySpinner.add((CityFind) iterator);
					CityFind item = (CityFind) iterator;
					Log.d(tag, "Lista item: " + item.toString());
				}

				ArrayAdapter<CityFind> adapter = new ArrayAdapter<CityFind>(
						this, android.R.layout.simple_spinner_item,
						arraySpinner);
				citySelector.setAdapter(adapter);
				citySelectorText.setVisibility(View.VISIBLE);
				citySelector.setVisibility(View.VISIBLE);
			} else {
				jump.setEnabled(false);
			}
			search.setText(R.string.buttonTextSearchAgain);
			progressBar.setVisibility(View.INVISIBLE);

			break;
		case ERROR:
			progressBar.setVisibility(View.INVISIBLE);
			// handle the error;
			break;
		}
	}
}
